package com.scwatch.core.service.impl;

import com.scwatch.common.orm.Limitable;
import com.scwatch.common.orm.SearchFilter;
import com.scwatch.core.domain.CodeDev;
import com.scwatch.core.repository.CodeDevDao;
import com.scwatch.core.service.CodeDevService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
public class CodeDevServiceImpl implements CodeDevService {
    @Override
    public Page<CodeDev> findAll(Map<String, String[]> params, Pageable pageable) {
        //注意 codeId.id
        return codeDevDao.findAll(spec(null, params), pageable);
    }

    @Override
    public List<CodeDev> findAll(Map<String, String[]> params, Limitable limitable) {
        //注意 codeId.id
        return codeDevDao.findAll(spec(null, params), limitable);
    }

    @Override
    @Transactional
    public CodeDev saveOrUpdata(CodeDev bean) {
        bean = codeDevDao.save(bean);
        em.flush();
        em.refresh(bean);
        return bean;
    }

    @Override
    public CodeDev findById(Integer id) {
        return codeDevDao.findOne(id);
    }

    @Override
    public void delete(Integer[] ids) {
        if (ids != null) {
            for (Integer id : ids) {
                CodeDev codeDev = findById(id);
                codeDevDao.delete(codeDev);
            }
        }
    }

    @Override
    public int deleteCodeIds(Integer[] codeIds) {
       return  codeDevDao.deleteByCodeDevId(Arrays.asList(codeIds));
    }

    private Specification<CodeDev> spec(final Integer codeId, Map<String, String[]> params) {
        Collection<SearchFilter> filters = SearchFilter.parse(params).values();
        final Specification<CodeDev> fsp = SearchFilter.spec(filters, CodeDev.class);
        Specification<CodeDev> sp = new Specification<CodeDev>() {
            public Predicate toPredicate(Root<CodeDev> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate pred = fsp.toPredicate(root, query, cb);
//                if (codeId != null) {
//                    pred = cb.and(pred, cb.equal(root.get("codeId").get("id"), codeId));
//                }
                return pred;
            }
        };
        return sp;
    }

    @Autowired
    private CodeDevDao codeDevDao;

    @Autowired
    private EntityManager em;

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }
}
